//
//  LXVisitorView.swift
//  weibo
//
//  Created by lixin on 16/4/16.
//  Copyright © 2016年 lixin. All rights reserved.
//

import UIKit

import SnapKit

class LXVisitorView: UIView {

    var visitorClosure:(() -> ())?
    
    override init(frame: CGRect) {
        
        super.init(frame: frame)
        
        setUI()
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        
        fatalError("init(coder:) has not been implemented")
    }
    
    //MARK: - 外界调用的方法
    func setVisitorInfo(imgName imgName: String?, title: String?){
    
        if let img = imgName, tit = title{
        
            houseImageView.image = UIImage(named: img)
            
            messageLabel.text = tit
            
            circleImageView.hidden = true
       
        }else{
        
            setCircleAnimation()
        }
    
    }
    
    //MARK: - 圆圈转
    private func setCircleAnimation(){
    
        let animation = CABasicAnimation(keyPath:"transform.rotation")
        
        animation.duration = 15
        
        animation.toValue = 2 * M_PI
        
        animation.repeatCount = MAXFLOAT
        
        animation.removedOnCompletion = false//切换tabbar或退出后台时，动画移除
        
        circleImageView.layer.addAnimation(animation, forKey: nil)
    }
    
    //MARK: - 设置UI
    
    private func setUI() {
    
        self.backgroundColor = RGB(red: 237, green: 237, blue: 237)
        
        addSubview(circleImageView)
        
        addSubview(maskImageView)
        
        addSubview(houseImageView)
        
        addSubview(messageLabel)
        
        addSubview(registerBtn)
        
        addSubview(loginBtn)
        
        circleImageView.snp_makeConstraints { (make) in
            
            make.center.equalTo(self)
        }
        
        maskImageView.snp_makeConstraints { (make) in
            
            make.centerX.equalTo(self)
            
            make.centerY.equalTo(self)
        }
        
        houseImageView.snp_makeConstraints { (make) in
            
            make.center.equalTo(self)
        }
        
        messageLabel.snp_makeConstraints { (make) in
            
            make.top.equalTo(maskImageView.snp_bottom).offset(16)
            
            make.centerX.equalTo(maskImageView)
            
            make.height.equalTo(36)
            
            make.width.equalTo(230)
        }
        
        registerBtn.snp_makeConstraints { (make) in
            
            make.leading.equalTo(messageLabel)
            
            make.top.equalTo(messageLabel.snp_bottom).offset(16)
            
            make.size.equalTo(CGSize(width: 100, height: 36))
        }
        
        loginBtn.snp_makeConstraints { (make) in
            
            make.trailing.equalTo(messageLabel)
            
            make.top.equalTo(registerBtn)
            
            make.size.equalTo(CGSize(width: 100, height: 36))
        }
        
    }
    
    //MARK: - 懒加载控件
    
    //圆圈
    private lazy var circleImageView = UIImageView(image:UIImage(named: "visitordiscover_feed_image_smallicon"))
    
    //遮盖
    private lazy var maskImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    
    //小房子
    private lazy var houseImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    
    //文字
    private lazy var messageLabel:UILabel = {
    
        let label = UILabel(textColor: UIColor.grayColor(), textFont: 14)
        
        label.textAlignment = .Center
        
        label.numberOfLines = 0
        
        label.text = "关注一些人，回这里看看有什么惊喜关注一些人，回这里看看有什么惊喜"
        
        return label
    }()
    
    //注册按钮
    private lazy var registerBtn: UIButton = {
    
        let btn = UIButton()
        
        btn.addTarget(self, action:#selector(LXVisitorView.loginClick) , forControlEvents: UIControlEvents.TouchUpInside)
        
        btn.setBackgroundImage(UIImage(named:"common_button_white_disable"), forState: UIControlState.Normal)
        
        btn.setTitle("注册", forState: UIControlState.Normal)
        
        btn.setTitleColor(ThemeColor, forState: UIControlState.Normal)
        
        btn.titleLabel?.font = UIFont.systemFontOfSize(14)
        
        return btn
    }()
    
    //登录按钮
    private lazy var loginBtn: UIButton = {
        
        let btn = UIButton()
        
        btn.addTarget(self, action:#selector(LXVisitorView.loginClick) , forControlEvents: UIControlEvents.TouchUpInside)
        
        btn.setBackgroundImage(UIImage(named:"common_button_white_disable"), forState: UIControlState.Normal)
        
        btn.setTitle("登录", forState: UIControlState.Normal)
        
        btn.setTitleColor(ThemeColor, forState: UIControlState.Normal)
        
        btn.titleLabel?.font = UIFont.systemFontOfSize(14)
        
        return btn
    }()
    
    //MARK: - 点击事件
    @objc private func loginClick(){
    
        visitorClosure?()
    }
}
